package proj.client;

import java.io.*;
import java.net.*;
import java.util.*;

import proj.common.*;
import proj.novel.*;

public class ClientSide {
	private static ObjectInputStream ois = null;
	private static ObjectOutputStream oos = null;

	public static Response writeObj(Request req) {
		Response res = null;
		try {
			oos.writeObject(req);
			oos.flush();
			oos.reset();
			res = (Response) ois.readObject();
			return res;
		} catch (Exception e) {
			e.printStackTrace();
			return new Response(false, "网络故障！");
		}
	}

	public static void main(String[] args) {
		System.out.println("启动客户端");
		Socket s = null;
		InputStream is = null;
		OutputStream os = null;
		Request req = null;
		Response res = null;
		try {
			s = new Socket("localhost", 5555);
			is = s.getInputStream();
			os = s.getOutputStream();
			ois = new ObjectInputStream(is);
			oos = new ObjectOutputStream(os);
			Scanner in = new Scanner(System.in);
			boolean flag1 = false;
			while (true) {
				System.out.println("********************************");
				System.out.println("1.注册,2.登陆,3.退出");
				System.out.println("********************************");
				System.out.print("请选择：");
				int ch = in.nextInt();
				switch (ch) {
				case 1: {
					req = new Request("register");
					String name = null;
					String psd = null;
					String repsd = null;
					System.out.println("输入用户名：");
					name = in.next();
					while (true) {
						System.out.println("输入密码：");
						psd = in.next();
						System.out.println("确认密码：");
						repsd = in.next();
						if (psd.equals(repsd))
							break;
						else
							System.out.println("两次密码不一致，请重新输入：");
					}
					req.put("userName", name);
					req.put("userPass", psd);
					res = writeObj(req);
					System.out.println(res.getMsg());
					break;
				}
				case 2: {
					req = new Request("login");
					System.out.println("输入用户名：");
					String name = in.next();
					System.out.println("输入密码：");
					String psd = in.next();
					req.put("userName", name);
					req.put("userPass", psd);
					res = writeObj(req);
					boolean islogin = res.isSucess();
					System.out.println(res.getMsg());
					boolean flag2 = false;
					while (islogin) {
						System.out.println("***********************************");
						System.out.println("1.修改密码,2查看所有小说,0.返回上一级");
						System.out.println("***********************************");
						int ch1 = in.nextInt();
						switch (ch1) {
						case 1: {
							req = new Request("changePass");
							String newpass = null;
							while (true) {
								System.out.println("请输入新密码：");
								newpass = in.next();
								System.out.println("请确认新密码：");
								String newpass1 = in.next();
								if (newpass.equals(newpass1))
									break;
								else
									System.out.println("两次密码不一致，请重新输入：");
							}
							req.put("userName", name);
							req.put("userPass", newpass);
							res = writeObj(req);
							System.out.println(res.getMsg());
							break;
						}
						case 2: {
							req = new Request("fileInfo");
							res = writeObj(req);
							@SuppressWarnings("unchecked")
							List<Novel> list = (List<Novel>) res.get("fileInfo");
							System.out.println("序号\t小说名\t\t作者\t\t简介");
							Iterator<Novel> iterator = list.iterator();
							System.out.println("*****************小说信息*****************");
							while (iterator.hasNext()) {
								Novel n = iterator.next();
								System.out.println("-" + (n.getNo()) + "-\t" + n.getName() + "\t\t" + n.getAuthor()
										+ "\t\t" + n.getIntro());
							}
							System.out.println("**********************************");
							boolean falg3 = false;
							while (true) {
								System.out.println("输入9上传小说,输入小说序号进行阅读和下载，输入0返回上一级");
								System.out.print("请输入选择：");
								int ch2 = in.nextInt();
								switch (ch2) {
								case 9: {
									req = new Request("upload");
									System.out.println("输入小说名：");
									String novelName = in.next();
									System.out.println("输入作者：");
									String author = in.next();
									System.out.println("输入简介：");
									String introduce = in.next();
									System.out.println("输入文件路径：");
									String path = in.next();
									Novel novel = new Novel();
									novel.setName(novelName);
									novel.setAuthor(author);
									novel.setIntro(introduce);
									File f = new File(path);
									if (!f.exists()) {
										System.out.println("文件不存在！");
									} else if (!f.getName().endsWith("txt")) {
										System.out.println("格式不正确");
									} else {
										byte[] b = new byte[(int) f.length()];
										FileInputStream fis = new FileInputStream(f);
										fis.read(b);
										fis.close();
										req.put("novel", novel);
										req.put("fileContent", b);
										res = writeObj(req);
										System.out.println(res.getMsg());
									}
									break;
								}
								case 0: {
									falg3 = true;
									break;
								}
								default: {
									req = new Request("fileInfo");
									res = writeObj(req);
									@SuppressWarnings("unchecked")
									List<Novel> novels = (List<Novel>) res.get("fileInfo");
									if (ch2 <= novels.size()) {
										req = new Request("getContent");
										req.put("filename", novels.get(ch2 - 1).getName());
										res = writeObj(req);
										byte b[] = (byte[]) res.get("content");
										int co = 1;
										boolean flag4 = false;
										String con = new String(b);
										String read = con.substring(0, 256);
										System.out.println("**********************************");
										System.out.println(read);
										System.out.println("**********************************");
										while (true) {
											System.out.println("输入0返回上一级，输入1下载，输入其他继续阅读");
											int ch4 = in.nextInt();
											switch (ch4) {
											case 1: {
												req = new Request("getContent");
												req.put("filename", novels.get(ch2 - 1).getName());
												res = writeObj(req);
												byte[] content = (byte[]) res.get("content");
												File f = new File("src/proj/client/", res.getMsg() + ".txt");
												FileOutputStream fos = new FileOutputStream(f);
												fos.write(b);
												fos.close();
												System.out.println("《" + res.getMsg() + "》下载成功！");
												break;
											}
											case 0: {
												flag4 = true;
												break;
											}
											default: {
												read = con.substring(256 * co, 256 * (co + 1));
												System.out.println("*****************在线阅读*****************");
												System.out.println(read);
												System.out
														.println("***************************************************");
												co++;
												break;
											}
											}
											if (flag4)
												break;
										}
									} else {
										System.out.println("没有此小说！");
									}
								}
								}
								if (falg3)
									break;
							}
							break;
						}
						case 0: {
							flag2 = true;
							break;
						}
						default: {
							System.out.println("请再选择！");
							break;
						}
						}
						if (flag2)
							break;
					}
					break;
				}
				case 9: {
					req = new Request("quit");
					res = writeObj(req);
					System.out.println("感谢使用");
					flag1 = true;
					break;
				}
				default: {
					System.out.println("功能尚在完善");
					break;
				}
				}
				if (flag1)
					break;
			}

		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				oos.close();
				ois.close();
				s.close();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}
}
